import express from 'express';
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import cookieSession from 'cookie-session';
import uuid from 'uuid';
import multer from 'multer';
import consolidate from 'consolidate';
import logger from 'morgan';
var cors=require('cors'); //允许跨域组件 

let multerObj=multer({dest:"./public/upload"})

let app=express();
app.use(express.static("./public"));
app.listen(8080);

var whitrlist=['http://127.0.0.1:8080','http://localhost:8080']

//白名单
var corsOptions={
    "origin":"*",
    "methods":"GET,HEAD,PUT,PATCH,POST,DELETE",
    "preflightContinue":false,
    "optionsSuccessStatus":204
}


app.use(cors(corsOptions)) //可以跨域的白名单

//日志
app.use(logger("dev"));
app.use(logger("combined",{stream:require("fs").createWriteStream("./logs/logs.log",{flags:"a"})}));


app.use(multerObj.any()); //any 所有的都能支持


//中间件的注入
app.use(bodyParser.urlencoded({extended:true,limit:"500mb"}));
app.use(bodyParser.json());
app.use(bodyParser.text());

app.use(cookieParser("cs1803"));
app.use(cookieSession({
    genid:function(){
        return uuid.v1(); //生成的唯一码
    },
    maxAge:60*1000*20,
    secret:"cs1803"
}))


//模板引擎
app.set("view engine","html");
app.set("views","./views");
app.engine("html",consolidate.ejs);

//引入路由
app.use("/admin",require("./router/admin/adminRouter"));
